<?php
declare(strict_types=1);

namespace App\Model\BbcModel;

use Service\Code\Normal;
use App\Model\BaseModel\BaseBbcModel;

class CategoryBanner extends BaseBbcModel {
    public $table = 'ly_category_banner';

    /**
     * 获取某一个分类Banner详情
     */
    public static function getDetail($id = 0, $type = true) {
        if (!$id) {
            return Normal::returnCode(Normal::FAIL, 'id不能为空');
        }
        $info = self::where('id', $id)->first();
        if (!$info) {
            return Normal::returnCode(Normal::FAIL, '未找到相关数据');
        }
        $info    = $info->toArray();
        $banners = CategoryBannerDetail::where('category_banner_id', $id)->orderBy('show_order', 'asc')->get();
        if ($banners->isEmpty()) {
            return Normal::returnCode(Normal::FAIL, '未找到相关详情数据');
        }
        $goods_ids = $banners->where('object_type', 1)->pluck('object_id')->toArray();
        $goodsAll  = [];
        if ($goods_ids) {
            $goodsAll = Goods::whereIn('id', $goods_ids)->pluck('name', 'id')->toArray();
        }
        $activity_ids = $banners->whereIn('object_type', [5, 6, 7])->pluck('object_id')->toArray();
        $activityAll  = [];
        if ($activity_ids) {
            $activityAll = Activity::whereIn('id', $activity_ids)->pluck('name', 'id')->toArray();
        }
        $class_ids = $banners->where('object_type', 3)->pluck('object_id')->toArray();
        $classAll  = [];
        if ($class_ids) {
            $classAll = GoodsClass::whereIn('id', $class_ids)->pluck('name', 'id')->toArray();
        }
        $banner = [];
        foreach ($banners as $key => $value) {
            $content = '';
            if ($value['object_type'] == 1) {
                $content = $goodsAll[$value['object_id']] ?? '';
            }
            if (in_array($value['object_type'], [5, 6, 7])) {
                $content = $activityAll[$value['object_id']] ?? '';
            }
            if ($value['object_type'] == 3) {
                $content = $classAll[$value['object_id']] ?? '';
            }
            $detail = [
                'id'          => $value['id'],
                'img'         => $value['img_url'],
                'object_id'   => $value['object_id'],
                'object_type' => $value['object_type'],
                'content'     => $content,
            ];
            if ($type) {
                $banner[$key + 1] = $detail;
            } else {
                $banner[] = $detail;
            }
        }
        $info['banner'] = $banner;
        return Normal::returnCode(Normal::SUC, '成功', $info);
    }
}